﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xml = System.Xml.Serialization;
using System.IO;

namespace Utilities.Runtime.Serialization
{
    public class XmlSerializer : ISerializer
    {
        public string Serialize(object data)
        {
            Xml.XmlSerializer serializer = new Xml.XmlSerializer(data.GetType());

            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, data);

            return Encoding.Default.GetString(stream.ToArray());
        }

        public T Deserialize<T>(string data)
        {
            Xml.XmlSerializer serializer = new Xml.XmlSerializer(typeof(T));
            
            byte[] dataBytes = Encoding.Default.GetBytes(data);
            MemoryStream stream = new MemoryStream(dataBytes);

            return (T)serializer.Deserialize(stream);
        }
    }
}
